package com.tensquare.article.controller;

import com.tensquare.entity.Result;
import com.tensquare.entity.StatusCode;
import com.tensquare.exception.TensquareException;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;


@RestControllerAdvice // 拦截controller
//@ControllerAdvice
public class MyExceptionHandler {

    // 按类型捕获异常
    @ExceptionHandler(Exception.class)
    public Result handleExcetpion(Exception e){
        e.printStackTrace();// 企业项目中，不能使用这句。会调用控制台的输出流(占用大量jvm资源)，使用log来替换
        return new Result(false, StatusCode.ERROR,"发生异常，请联系管理员");
    }

    @ExceptionHandler(TensquareException.class)
    public Result handleTensquareException(TensquareException e){
        return new Result(false, StatusCode.ERROR,e.getMessage());
    }
}
